#include "stdafx.h"
#include "ProLoad.h"

#include <windows.h>

#define DE
#include "..\\..\\STD.h"
#include "..\\..\\proc.h"

int StartProc(CHAR Proc[], DWORD dwShow)
{
	CHAR ProcTmp[1024];
	lstrcpyA(ProcTmp, Proc);

	for (int i = lstrlenA(ProcTmp) - 1; i > 0; i--)
	{
		if (ProcTmp[i] == ' ')
		{
			ProcTmp[i] = '\0';
			break;
		}
	}

	if (!ProcPathExist(ProcTmp))
	{
		CreateProc(Proc, dwShow);
	}

	return 0;
}

DWORD LoadProc(TCHAR File[], DWORD dwShow, DWORD dwIntervel)
{
	CHAR Data[4096], *stPos = Data;
	int i;

	ZeroMemory(Data, 4096);
	ReadTxt(File, Data, 4096);

	if (Data[0] < 0)
	{
		stPos = Data + 3;
	}

	for (i = 0; i < 4095; i++)
	{
		if (Data[i] == '\r')
		{
			Data[i] = '\0';

			StartProc(stPos, dwShow);
			
			stPos = &Data[i + 2];

			Sleep(dwIntervel);
		}
	}

	StartProc(stPos, dwShow);

	return 0;
}

TCHAR Path[256];

int LoadFile(TCHAR FN[], DWORD dwShow, DWORD dwSleep, DWORD dwIntervel)
{
	TCHAR LoadNow[256];

	Sleep(dwSleep * 1000);
	lstrcpy(LoadNow, Path);
	lstrcat(LoadNow, FN);

	return LoadProc(LoadNow, dwShow, dwIntervel);
}

int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPTSTR    lpCmdLine,
	_In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

	int i;

	GetModuleFileNameEx(GetCurrentProcess(), NULL, Path, 256);

	for (i = lstrlen(Path); i > 0; i--)
	{
		if (Path[i] == '\\')
		{
			Path[i+1] = '\0';
			break;
		}
	}

	LoadFile(L"proload.txt", SW_MINIMIZE, 0, 100);

	LoadFile(L"prodelay.txt", SW_MINIMIZE, 10, 1000);

	
	if ( NetConnected())
	{
		LoadFile(L"profin.txt", SW_MINIMIZE, 60, 10 * 1000);
	}
	
	return 0;
}
